include ../CommonMakefile

JAR         = transport.jar
# TODO: transport should not compile COMMONSRC
COMMONSRC   = ../Common.scala ../Debug.scala
TESTSRC     = $(wildcard test/*.scala)
MAINSRC     = $(wildcard main/*.scala) $(COMMONSRC)

LIB         = ../lib
SCALATEST   = scalatest-1.0
EASYMOCK    = easymock-2.5.2
EASYMOCKCE  = easymockclassextension-2.5.2
CGLIB	    = cglib-nodep-2.2.jar

LIBJAR      = $(LIB)/$(1)/$(1).jar
SCALACP     = $(LIB)/scala-library.jar
SCALATESTCP = $(call LIBJAR,$(SCALATEST))
EASYMOCKCP  = $(call LIBJAR,$(EASYMOCK))$(PATHSEP)$(call LIBJAR,$(EASYMOCKCE))$(PATHSEP)$(LIB)/$(CGLIB)

MAINCP      = -classpath $(SCALACP)
TESTCP      = $(MAINCP)$(PATHSEP)$(SCALATESTCP)$(PATHSEP)$(EASYMOCKCP)$(PATHSEP)test/test.jar

COPTS       = -deprecation


all: $(JAR)

stable: all

test: TEST.out 

api: api/index.html

TEST.out: TEST.classes
	@echo "Starting server runner, which will kill itself after a while..."
	scala $(TESTCP)$(PATHSEP). test/startServerRunner.script 60 &
	@echo "Pausing to let server runner start up..."
	sleep 5
	scala $(TESTCP) org.scalatest.tools.Runner -o -p . |\
			    tee $@ 2>&1 | grep -v 'at .*[0-9])' | grep -v '^--'
	@echo "Killing the startServerRunner process."
	-kill `pgrep -f startServerRunner`

$(JAR): MAIN.out
	jar cf $@ `find . -name '*.class'`

MAIN.out: MAIN.classes
	@touch $@

MAIN.classes: $(MAINSRC)
	$(SCALAC) $(COPTS) $(MAINCP) $(MAINSRC)
	@touch $@

TEST.classes: $(TESTSRC) MAIN.classes
	$(SCALAC) $(COPTS) $(TESTCP) $(TESTSRC)
	@touch $@

api/index.html: $(MAINSRC)
	mkdir -p api
	scaladoc -access:private -d api $(MAINSRC)

clean:
	rm -rf *~ */*~ *.class *.classes *.out edu $(JAR)

.PHONY: all api clean stable test
